package double_pointer;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-12-02
 * Time: 22:54
 */

public class RotateRight {
    public static ListNode rotateRight(ListNode head, int k) {
        if (head == null || k == 0 || head.next == null) {
            return head;
        }
        int length = 0;
        ListNode tmp = new ListNode(0, head);
        while (tmp.next != null) {
            length++;
            tmp = tmp.next;
        }
        k %= length;
        if (k == 0) {
            return head;
        }
        tmp.next = head;
        for (int i = 0; i < length - k; i++) {
            tmp = tmp.next;
        }
        head = tmp.next;
        tmp.next = null;
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(2);
//        ListNode listNode2 = new ListNode(3);
//        ListNode listNode3 = new ListNode(4);
//        ListNode listNode4 = new ListNode(5);
        listNode.next = listNode1;
//        listNode1.next = listNode2;
//        listNode2.next = listNode3;
//        listNode3.next = listNode4;
        listNode1.next = null;
        rotateRight(listNode, 2);
    }
}
